package com.hans.scala.ImplicitConversion

/**
 *
 * Created by Juntao.Zhang on 2014/4/29.
 */
object OrderingExample extends App {

  import scala.util.Sorting

  val people = Array(Person("bob", 30), Person("ann", 32), Person("carl", 19))
  val pairs = Array(("a", 5, 2), ("c", 3, 1), ("b", 1, 3))

  case class Person(name: String, age: Int)

  Sorting.quickSort(people)(AgeOrdering)


  import scala.util.Sorting

  // sort by age
  object AgeOrdering extends Ordering[Person] {
    def compare(a: Person, b: Person) = a.age compare b.age
  }

  // sort by 2nd element
  Sorting.quickSort(pairs)(Ordering.by[(String, Int, Int), Int](_._2))

  // sort by the 3rd element, then 1st
  Sorting.quickSort(pairs)(Ordering[(Int, String)].on(x => (x._3, x._1)))
}
